using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using DG.Tweening;

namespace Demo
{
    public class Hero1HandCards : MonoBehaviour
    {
        public GameObject GOHandCardPrefab; //手牌预设 
        private List<GameObject> ListHandCard = new List<GameObject>(); //手牌列表 
        public GameObject TransBeginHandCard; //生成手牌的最开始的位置

        private float _FloRotateAngel; //手牌动画旋转的角度

// Use this for initialization
        void Start()
        {
        }

// Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                GetCards();
                RotateAngel();
                AddCardAnimations();
            }
            else if (Input.GetKeyDown(KeyCode.Backspace))
            {
                UseCardAnimation();
            }
        }

        public void GetCards()
        {
            //克隆预设
            GameObject GOHandCard = GameObject.Instantiate(GOHandCardPrefab) as GameObject;
            GOHandCard.transform.position = TransBeginHandCard.transform.position;
            GOHandCard.transform.parent = this.transform;
            //将新手牌添加到手牌列表
            ListHandCard.Add(GOHandCard);
            //计算动画需要旋转的角度
            RotateAngel();
        }

//为手牌添加动画
        private void HandCardAnimation(GameObject GO, float Vec3_Z)
        {
            GO.transform.DORotate(new Vector3(0, 0, Vec3_Z), 0.3F, RotateMode.Fast);
        }

//增加手牌时播放的动画
        private void AddCardAnimations()
        {
            if (ListHandCard.Count == 1)
            {
                HandCardAnimation(ListHandCard[0], 0);
            }
            else
            {
                for (int i = 1; i < ListHandCard.Count; i++)
                {
                    HandCardAnimation(ListHandCard[i - 1],
                        30 - _FloRotateAngel * (float) i * ListHandCard.Count + 2.5F);
                }

                HandCardAnimation(ListHandCard[ListHandCard.Count - 1], -27.5F + _FloRotateAngel);
//                HandCardAnimation(ListHandCard[ListHandCard.Count - 1], -30F + _FloRotateAngel);
            }
        }

//使用手牌时播放的动画
        private void UseCardAnimation()
        {
            Destroy(ListHandCard[0]);
            ListHandCard.Remove(ListHandCard[0]);
            RotateAngel();
            if (ListHandCard.Count == 1)
            {
                HandCardAnimation(ListHandCard[0], 0);
            }
            else if (ListHandCard.Count > 1)
            {
                for (int i = 1; i < ListHandCard.Count + 1; i++)
                {
                    HandCardAnimation(ListHandCard[i - 1],
                        30 - _FloRotateAngel * (float) i * ListHandCard.Count + 2.5F);
                    HandCardAnimation(ListHandCard[ListHandCard.Count - 1], -27.5F + _FloRotateAngel);
                }
            }
        }

//计算需要旋转的角度
        private void RotateAngel()
        {
            _FloRotateAngel = 55F / (float) ListHandCard.Count / (float) ListHandCard.Count;
        }
    }
}